import streamlit as st
from streamlit_autorefresh import st_autorefresh
from controller.ui_controller import UIController
from controller.email_controller import EmailController
from views.results_view import ResultsView

# 条件性自动刷新 - 只在非结果查询页面刷新
current_menu = st.session_state.get('current_menu', 'dashboard')
if current_menu != 'results':
    st_autorefresh(interval=5000, debounce=True, key="uniqueKey")

# Streamlit 页面配置
st.set_page_config(
    page_title="EmlAIdata监控面板",
    page_icon="📧",
    layout="wide",
    initial_sidebar_state="expanded"
)

# 自定义CSS样式
st.markdown("""
<style>
    .main-header {
        color: #1f77b4;
        text-align: center;
        padding: 1rem 0;
        border-bottom: 2px solid #e0e0e0;
        margin-bottom: 2rem;
    }
    
    .sidebar-title {
        color: #2e8b57;
        text-align: center;
        padding: 1rem 0;
        border-bottom: 1px solid #e0e0e0;
        margin-bottom: 1rem;
    }
    
    .metric-container {
        background-color: #f8f9fa;
        padding: 1rem;
        border-radius: 0.5rem;
        border-left: 4px solid #1f77b4;
        margin: 0.5rem 0;
    }
    
    .status-success {
        color: #28a745;
        font-weight: bold;
    }
    
    .status-error {
        color: #dc3545;
        font-weight: bold;
    }
    
    .status-warning {
        color: #ffc107;
        font-weight: bold;
    }
    
    .log-entry {
        padding: 0.5rem;
        margin: 0.25rem 0;
        border-radius: 0.25rem;
        font-family: monospace;
        font-size: 0.9rem;
    }
    
    .log-info {
        background-color: #e3f2fd;
        border-left: 3px solid #2196f3;
    }
    
    .log-warning {
        background-color: #fff3e0;
        border-left: 3px solid #ff9800;
    }
    
    .log-error {
        background-color: #ffebee;
        border-left: 3px solid #f44336;
    }
    
    .stButton > button {
        width: 100%;
    }
    
    .stSelectbox > div > div {
        background-color: #ffffff;
    }
</style>
""", unsafe_allow_html=True)

def main():
    """主函数 - MVC架构入口"""

    # 创建UI控制器
    ui_controller = UIController()
    # 创建邮件控制器
    email_controller = EmailController()    
    ResultsView.set_email_controller(email_controller)
    # 渲染页面
    ui_controller.render_page()

if __name__ == "__main__":
    main()